

clear all
************************************
*Numeric Export disabled
local TEX ""
local TEXON 1 //Change "0" to "1" to enable exporting numeric results to .txt files
if !`TEXON' local TEX "*"
*Also need to install `texresults2
*ssc install texresults2
************************************

*************************
*set working directory
*************************

*Use data from Page, Bartels, and Seawright 2013
import excel using "Enns_SociologicalScience_Reproduction\TablesFromPageBartelsSeawright2013.xlsx", clear firstrow

cor Superrich GeneralPublic
mat cor = r(C)
local cor_sr_gp = cor[2,1]
di `cor_sr_gp'

*obtain observed mean and sd to use in simulations
//To keep mean and standard deviation comparable to Gilens data, questions that Page, Bartels, and Seawright (2013) report based on calculation (i.e., percent "expanded" minus percent "cut back," percent need more - percent need less, ratio of median "ought" to median "actual")
gen propsuperrich = Superrich/100
sum propsuperrich if Percent==1
local meansuperrich = r(mean)
local sdsuperrich = r(sd)
di `meansuperrich', `sdsuperrich'

*obtain observed mean and sd from Gilens' data
use "Enns_SociologicalScience_Reproduction\DS1_0.dta", clear 
sum pred50_sw 
local mean50 = r(mean)
local sd50 = r(sd)
sum pred90_sw
local mean90 = r(mean)
local sd90 = r(sd)
di `mean50', `sd50', `mean90', `sd90'

cor pred50_sw pred90_sw
mat cor = r(C)
local cor_50_90 = cor[2,1]
di `cor_50_90'

//Use simulations to evaluate ability of Gilens' affluent (90th percentile) to serve as a proxy for the super rich

//Correlation b/t affluent (90th) and super rich is set to r=0.95. This relationship must be greater than the relationship beween the affluent and the median for the affluent to serve as a better proxy of the preferences of the super rich than of the preferences of the median.
mat C = (1, .95, `cor_sr_gp' \ .95, 1, `cor_50_90' \ `cor_sr_gp', `cor_50_90', 1)
mat rownames C = superrich affluent mid
mat colnames C = superrich affluent mid
mat l C

clear
set obs 2000
capture noisily drawnorm superrich affluent mid, means (`meansuperrich', `mean90', `mean50') sds (`sdsuperrich', `sd90', `sd50') corr(C) clear
//The above produces an error, suggesting with these three variables it is not possible for the association between the preferences of the affluent (90th) and superrich to be greater than the assocation between the prefrences of the affluent and the median.

//Retest setting the association between the affluent and super rich just .001 greater than the association between the affluent and the median
local cor_50_90b = `cor_50_90' + 0.001
di `cor_50_90b'
mat C = (1, `cor_50_90b', `cor_sr_gp' \ `cor_50_90b', 1, `cor_50_90' \ `cor_sr_gp', `cor_50_90', 1)
mat rownames C = superrich affluent mid
mat colnames C = superrich affluent mid
mat l C
clear
set obs 2000
capture noisily drawnorm superrich affluent mid, means (`meansuperrich', `mean90', `mean50') sds (`sdsuperrich', `sd90', `sd50') corr(C) clear
//This combination of relationships also cannot exist. 

//Confirm by plotting eigen values, some of which are negative
matrix symeigen values vectors = C
matrix list values

`TEX'texresults2 using TxtFiles_NumericalResults\results.txt, texmacro(corpubsuperrich) result(`cor_sr_gp') round(2) append 

